#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'HaiFeng'
__mtime__ = '2016/8/16'
"""
from py_at.adapters.ctp_trade import *
from py_at.adapters.ctp_quote import *



#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'HaiFeng'
__mtime__ = '2016/8/16'
http://blog.csdn.net/zhangzq86/article/details/49584043   CTP开发教程不错
"""
from py_at.adapters.ctp_trade import *
from py_at.adapters.ctp_quote import *
class Dome1():
    def __init__(self,userid,pwd):
        self.t=CtpTrade()
        self.q=CtpQuote()
        self.userid=userid
        self.pwd=pwd
        self.bangding()

     #----------------------------------------------------
    def bangding(self):
        """进行回调绑定以及发送链接前置主机"""
        self.q.OnFrontConnected = self.q_OnFrontConnected
        self.q.OnRspUserLogin = self.q_OnRspUserLogin

        self.t.OnFrontConnected = self.OnFrontConnected
        self.t.OnRspUserLogin = self.OnRspUserLogin
        self.t.OnRtnOrder = self.OnRtnOrder
        self.t.ReqConnect('tcp://180.168.146.187:10000')
    #--------------------------------------------------------
    def q_OnFrontConnected(self):
        print('{0}链接'.format(self.userid))
        self.t.ReqUserLogin(self.userid, '888888', '9999')

    # --------------------------------------------------------
    def q_OnRspUserLogin(self,info=InfoField):
        """"""
        print('{0}行情登陆成功'.format(self.userid))
        print(info)
        self.q.ReqSubscribeMarketData('rb1705')
        #for stra in self.stra_instances:
        #    q['051355'].ReqSubscribeMarketData(stra.Instrument)
    # --------------------------------------------------------
    def OnFrontConnected(self):
        print('{0}链接前置成功开始进行交易登陆'.format(self.userid))
        self.q.ReqUserLogin(self.userid, self.pwd, '9999')

    # --------------------------------------------------------
    def OnRspUserLogin(self,info=InfoField):
        """"""
        print(info)
        print('行情登陆')
        if info.ErrorID == 0:
            self.q.ReqConnect('tcp://180.168.146.187:10010')

    def OnRtnOrder(self, f=OrderField):
        """报单回答"""
        print('账户{0} 报单返回 {1}'.format(self.userid,f))

    #----------------------------------------------------------------------------------------------------------------------------------------------------
    # --------------------------------------------------------
    def q_OnFrontConnected(self):
        """"""
        print("开始行情进行账账户登陆")
        self.q.ReqUserLogin(self.userid, '888888', '9999')

if __name__ == '__main__':
    users={'104147':'nihao00o','051356':'888888'}

    for u,p in users.items():
        dome=Dome1(u,p)
        users[u]=dome

    time.sleep(20)
    print("登陆完成之后等待25秒")
    users['104147'].t.ReqOrderInsert('rb1801', DirectionType.Buy, OffsetType.Open, 3650, 1,
                          OrderType.Limit, 100)
    time.sleep(10)
    users['051356'].t.ReqOrderInsert('hc1801', DirectionType.Buy, OffsetType.Open, 3840, 1,
                                     OrderType.Limit, 100)
    input()



























"""  下面是单独例子模式

q = {}
t = {}
def q_OnFrontConnected():
    print('链接')
    q['051355'].ReqUserLogin('051355', '888888', '9999')

def q_OnRspUserLogin(info=InfoField):
    """"""
    print('行情登陆成功')
    print(info)
    # self.q.ReqSubscribeMarketData('rb1705')
    #for stra in self.stra_instances:
    #    q['051355'].ReqSubscribeMarketData(stra.Instrument)
def OnFrontConnected():
    print('链接前置成功开始进行交易登陆')
    t['051355'].ReqUserLogin('051355', '888888', '9999')

def OnRspUserLogin(info=InfoField):
    """"""
    print(info)
    print('行情登陆')
    if info.ErrorID == 0:
        q['051355'].ReqConnect('tcp://180.168.146.187:10010')

def q_OnFrontConnected():
    """"""
    print("开始行情进行账账户登陆")
    q['051355'].ReqUserLogin('051355', '888888', '9999')

if __name__ == '__main__':

    q['051355'] = CtpQuote()
    q['051355'].OnFrontConnected = q_OnFrontConnected
    q['051355'].OnRspUserLogin = q_OnRspUserLogin
    t['051355'] = CtpTrade()
    t['051355'].OnFrontConnected = OnFrontConnected
    t['051355'].OnRspUserLogin = OnRspUserLogin
    t['051355'].ReqConnect('tcp://180.168.146.187:10000')




    input()

"""